﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Plugin.Interface;
using Plugin.Manager;
using NET.Tools;
using NET.Tools.WPF;
using System.IO;

namespace Effect.FX.WPF
{
    /// <summary>
    /// Interaktionslogik für PluginExplorer.xaml
    /// </summary>
    public partial class PluginExplorer : Window
    {
        private ImageSource original;

        public PluginExplorer()
        {
            InitializeComponent();
            UpdateDLLList();

            original = imgPreview.Source.Clone();
        }

        private void UpdateDLLList()
        {
            var list = from value in Plugin.Manager.PluginManager.PluginEffectList 
                       orderby value.GetType().Assembly.Location ascending
                       group value by value.GetType().Assembly.Location;

            foreach (var group in list)
            {
                ListBoxItem lbi = new ListBoxItem();
                lbi.Content = new FileInfo(group.Key).Name;
                lbi.Tag = group.Key;

                lstDLL.Items.Add(lbi);
            }
        }

        private void UpdateEffectGroupList()
        {
            if (lstDLL.SelectedIndex < 0)
            {
                lstEffectGroup.Items.Clear();
                lstEffect.Items.Clear();
                lblDescription.Text = "";
                imgPreview.Source = original;
                return;
            }

            String asmName = (lstDLL.SelectedItem as ListBoxItem).Tag as String;

            var list = from value in Plugin.Manager.PluginManager.PluginEffectList
                       where value.GetType().Assembly.Location.EqualsIgnoreCase(asmName)
                       orderby value.Group.Name ascending
                       group value by value.Group;

            lstEffectGroup.Items.Clear();
            foreach (var group in list)
            {
                ImageListBoxItem lbi = new ImageListBoxItem();
                lbi.Icon = group.Key.Image.Source;
                lbi.Content = group.Key.Name;
                lbi.Tag = group.Key;

                lstEffectGroup.Items.Add(lbi);
            }
        }

        private void UpdateEffectList()
        {
            if (lstEffectGroup.SelectedIndex < 0)
            {
                lstEffect.Items.Clear();
                lblDescription.Text = "";
                imgPreview.Source = original;
                return;
            }

            EffectGroup effGroup = (lstEffectGroup.SelectedItem as ListBoxItem).Tag as EffectGroup;

            var list = from value in Plugin.Manager.PluginManager.PluginEffectList
                       where value.Group.Equals(effGroup)
                       orderby value.Name ascending
                       select value;

            lstEffect.Items.Clear();
            foreach (IEffect eff in list)
            {
                ImageListBoxItem lbi = new ImageListBoxItem();
                lbi.Icon = eff.Icon.Source;
                lbi.Content = eff.Name;
                lbi.Tag = eff;

                lstEffect.Items.Add(lbi);
            }
        }

        private void lblDLL_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateEffectGroupList();
        }        

        private void lstEffectGroup_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UpdateEffectList();
        }
        
        private void lstEffect_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstEffect.SelectedIndex < 0)
            {
                lblDescription.Text = "";
                imgPreview.Source = original;
                return;
            }

            IEffect eff = (lstEffect.SelectedItem as ListBoxItem).Tag as IEffect;

            lblDescription.Text = eff.Description;
            imgPreview.Source = eff.DoEffect(original as BitmapSource, 0);
        }
    }
}
